package kulstats;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;

public class TestDataFiller {
	private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	private static final ActivityManager actMger = Registry.activityManager();
	private static final UserManager usrMger = Registry.userManager();
	private static final QuestionnaireManager qstMger = Registry.questionnaireManager();
	private static final CourseManager crsMger = Registry.courseManager();
	
	static Random rnd = new Random();
	
	public static void fillAllTheThings(){
		qstMger.fill();
 		crsMger.fill();
		List<Course> allCrs = crsMger.getAllCourses();
		usrMger.registerUser("testuser01", "123456789", "", allCrs, UserType.STUDENT, "alles", "alles");
		Key usrKey = usrMger.getUser("123456789").getKey();
		int houroffset = 90*24;
		for (int i = 0; i < 50; i++){
			int rndAct = rnd.nextInt(3);
			if (rndAct == 0){
				int rndLss = rnd.nextInt(2);
				LessonType lt = LessonType.LECTURE;
				if (rndLss == 0){
					lt = LessonType.LECTURE;
				}else{
					lt = LessonType.PRACTICE;
				}
				actMger.saveActivity(generateLesson(rnd.nextInt(houroffset), lt, allCrs.get(rnd.nextInt(allCrs.size()))), usrKey);
			}else if(rndAct == 1){
				actMger.saveActivity(generateStudy(rnd.nextInt(houroffset), allCrs.get(rnd.nextInt(allCrs.size()))), usrKey);
			}else if(rndAct == 2){
				int rndStt = rnd.nextInt(5);
				SpareTimeType stt = SpareTimeType.EAT;
				if (rndStt == 0){
					stt = SpareTimeType.EAT;
				}else if(rndStt == 1){
					stt = SpareTimeType.HOBBY;
				}else if(rndStt == 2){
					stt = SpareTimeType.PARTY;
				}else if(rndStt == 3){
					stt = SpareTimeType.SLEEP;
				}else if(rndStt == 4){
					stt = SpareTimeType.SPORT;
				}
				actMger.saveActivity(generateSpareTime(rnd.nextInt(houroffset), stt), usrKey);
			}
		}
	}
	
	private static Lesson generateLesson(int houroffset, LessonType lt, Course crs){
		List<Question> listq = qstMger.manageQuestions(ActivityType.LESSON, null, lt);
		for (int i = 0; i < listq.size(); i++){
			listq.get(i).setAnswer(""+(rnd.nextInt(3)+rnd.nextInt(3)+rnd.nextInt(2)));
		}
		Calendar date = GregorianCalendar.getInstance();
		date.add(GregorianCalendar.HOUR, -houroffset);
		Calendar date2 = GregorianCalendar.getInstance();
		date2.add(GregorianCalendar.HOUR, -houroffset + rnd.nextInt(2) + 1);
		
		return new Lesson(date.getTime(), date2.getTime(), 0, listq, crs, lt);
	}
	
	private static Study generateStudy(int houroffset, Course crs){
		List<Question> listq = qstMger.manageQuestions(ActivityType.STUDY, null, null);
		for (int i = 0; i < listq.size(); i++){
			listq.get(i).setAnswer(""+(rnd.nextInt(4)+rnd.nextInt(3)));
		}
		Calendar date = GregorianCalendar.getInstance();
		date.add(GregorianCalendar.HOUR, -houroffset);
		Calendar date2 = GregorianCalendar.getInstance();
		date2.add(GregorianCalendar.HOUR, -houroffset + rnd.nextInt(2) + 1);
		
		int pauses = rnd.nextInt(3);
		int paused = 0;
		if (pauses != 0){
			paused = rnd.nextInt(45) + 10;
		}
		return new Study(date.getTime(), date2.getTime(), 0, listq, crs, paused, pauses, date2.getTime());
	}
	
	private static SpareTime generateSpareTime(int houroffset, SpareTimeType stt){
		List<Question> listq = qstMger.manageQuestions(ActivityType.SPARE_TIME, stt, null);
		for (int i = 0; i < listq.size(); i++){
			listq.get(i).setAnswer(""+(rnd.nextInt(3)+rnd.nextInt(2)+rnd.nextInt(2)+rnd.nextInt(2)));
		}
		Calendar date = GregorianCalendar.getInstance();
		date.add(GregorianCalendar.HOUR, -houroffset);
		Calendar date2 = GregorianCalendar.getInstance();
		date2.add(GregorianCalendar.HOUR, -houroffset + rnd.nextInt(2) + 1);
		
		return new SpareTime(date.getTime(), date2.getTime(), listq, stt);
	}
}
